package java114.jdbc;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo03_insert {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement statement = null;
        try{
            //1获取数据库连接
            connection = DBUtil.getConnection();

            //2定义sql语句
            String sql = "insert into bank_account (name, balance) values (?, ?)";
            //3预处理连接对象
            statement = connection.prepareStatement(sql);
            //4用户输入代替占位符
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入名字：");
            String name = scanner.next();
            System.out.println("请输入余额：");
            BigDecimal balance = scanner.nextBigDecimal();
            statement.setString(1,name);
            statement.setBigDecimal(2, balance);
            int row = statement.executeUpdate();
            if(row == 1){
                System.out.println("插入成功");
            }else{
                System.out.println("插入失败");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(null, statement, connection);
        }
    }


}
